// subPages/business/class/class.js
const app = getApp();
var util =  require('../../../utils/util.js');
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classId: '',
    phoneId: '',
    mainType: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      shopType: app.globalData.shopType
    })
    var title = ''
    var titleTxt = ''
    if(that.data.shopType == '3'){
      title = '便民热线'
      titleTxt = '添加热线'
    }else{
      title = '分类管理'
      titleTxt = '添加分类'
    }
    that.setData({
      titleTxt: titleTxt
    })
    util.setNavBarTitle(title)
    that.getClassData()
  },

  //特膳厨房分类按钮
  typeBtn:function(e){
    var that = this
    var type = e.currentTarget.dataset.type;
    that.setData({
      mainType: type
    })
    that.cancelBtn()
    that.getClassData()
  },

  //获取分类数据
  getClassData:function(){
    var that = this
    if(that.data.shopType == '0'){
      util.getAjax(app.globalData.apiUrl +'/get_community_service_shop_product_class',function(res){
        // console.log(res)
        if(res.data.length != 0){
          that.setData({
            classList: res.data
          })
        }
      },{shop_id: app.globalData.shopId})
    }else if(that.data.shopType == '2'){
      util.getAjax(app.globalData.apiUrl +'/get_community_service_kitchen_class',function(res){
        console.log(res)
        if(res.data.length != 0){
          that.setData({
            classList: res.data
          })
        }
      },{kitchen_id: app.globalData.shopId,type: that.data.mainType})
    }else if(that.data.shopType == '3'){
      util.getAjax(app.globalData.apiUrl +'/admin_get_community_phone',function(res){
        console.log(res)
        if(res.data.length != 0){
          that.setData({
            phoneList: res.data
          })
        }
      },{community_id: app.globalData.shopId})
    }
  },

  classInput:function(e){
    var that = this
    that.setData({
      className: e.detail.value
    })
  },

  nameInput:function(e){
    var that = this
    that.setData({
      phoneName: e.detail.value
    })
  },

  phoneInput:function(e){
    var that = this
    that.setData({
      phone: e.detail.value
    })
  },

  //点击修改
  classBtn:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var className = e.currentTarget.dataset.class
    that.setData({
      classId: id,
      className: className,
      titleTxt: '修改分类',
      isModify: true
    })
  },

  //点击修改
  phoneBtn:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var phoneName = e.currentTarget.dataset.name
    var phone = e.currentTarget.dataset.phone
    that.setData({
      phoneId: id,
      phoneName: phoneName,
      phone: phone,
      titleTxt: '修改热线',
      isModify: true
    })
  },

  //提交
  clickBtn:function(){
    var that = this
    console.log(flag)
    if(flag){
      return
    }
    flag = true
    var text = ''
    if(that.data.shopType != '3'){
      var data = {}
      var type = ''
      var url = ''
      if(!that.data.className){
        util.showModal('提示','请输入分类名称')
        flag = false
        return 
      }
      if(that.data.isModify){
        if(!that.data.classId){
          util.showModal('提示','请重新选择需要修改的分类')
          flag = false
          return
        }
        switch (that.data.shopType) {
          case '0':
            url = '/Api/Admincommunityserviceshop/operation_community_service_shop_class'
            data = {
              "id": that.data.classId,
              "class_name": that.data.className,
              "shop_id": app.globalData.shopId,
            }
            break;
          case '2':
            url = '/admin_operation_community_service_kitchen_class'
            data = {
              "id": that.data.classId,
              "class_name": that.data.className,
              "kitchen_id": app.globalData.shopId,
              "type": that.data.mainType
            }
            break;
        }
        text = '修改分类'
        type = '2'
      }else{
        //添加
        switch (that.data.shopType) {
          case '0':
            url = '/Api/Admincommunityserviceshop/operation_community_service_shop_class'
            data = {
              "class_name": that.data.className,
              "shop_id": app.globalData.shopId,
            }
            break;
          case '2':
            url = '/admin_operation_community_service_kitchen_class'
            data = {
              "class_name": that.data.className,
              "kitchen_id": app.globalData.shopId,
              "type": that.data.mainType
            }
            break;
        }
        type = '1'
        text = '添加分类'
      }
      data = JSON.stringify(data)
      // console.log(data)
      util.postAjax(app.globalData.apiUrl + url,function(res){
        // console.log(res)
        if(res.data == '1'){
          util.showToast(text+'成功','success',2000)
          that.cancelBtn()
          that.getClassData()
          flag = false
        }else{
          util.showToast(text+'失败，请重新提交','none',2000)
          flag = false
        }
      },{type: type,pro: data})
    }else{
      text = that.data.isModify?'修改热线':'添加热线'
      util.postAjax(`${app.globalData.apiUrl}/admin_operation_community_phone`,function(res){
        console.log(res)
        if(res.data == '1'){
          util.showToast(text+'成功','success',2000)
          that.cancelBtn()
          that.getClassData()
          flag = false
        }else{
          util.showToast(text+'失败，请重新提交','none',2000)
          flag = false
        }
      },{id: that.data.phoneId,phone_name: that.data.phoneName,phone: that.data.phone,community_id: app.globalData.shopId})
    }
    
  },

  //取消按钮
  cancelBtn:function(){
    var that = this
    if(that.data.shopType != '3'){
      that.setData({
        isModify: false,
        titleTxt: '添加分类',
        className: '',
        classId: ''
      })
    }else{
      that.setData({
        isModify: false,
        titleTxt: '添加热线',
        phoneName: '',
        phone: '',
        phoneId: '',
      })
    }
    
  },

  //删除按钮
  deleteBtn:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    if(that.data.shopType!='3'){
      var url = ''
      switch (that.data.shopType) {
        case '0':
          url = '/Api/Admincommunityserviceshop/operation_community_service_shop_class'
          break;
        case '2':
          url = '/admin_operation_community_service_kitchen_class'
          break;
      }
      util.postAjax(app.globalData.apiUrl + url,function(res){
        // console.log(res)
        if(res.data == '1'){
          util.showToast('此分类删除成功','success',2000)
          that.cancelBtn()
          that.getClassData()
        }else if(res.data == '2'){
          util.showToast('此分类下有商品，不可删除','none',2000)
        }else{
          util.showToast('此分类删除失败，请重新删除','none',2000)
        }
      },{type: 3,id: id})
    }else{
      util.postAjax(`${app.globalData.apiUrl}/admin_delete_community_phone`,function(res){
        // console.log(res)
        if(res.data == '1'){
          util.showToast('此热线删除成功','success',2000)
          that.cancelBtn()
          that.getClassData()
        }
      },{id: id})
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})